<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
  <div class="create-order">
    <s-header :name="'创建订单'" @callback="deleteLocal"></s-header>
    <div class="address-wrap">
      <div class="name" @click="goTo">
        <span>{{ address.name }} </span>
        <span>{{ address.tel }}</span>
      </div>
      <div class="address">
        {{ address.province_name }} {{ address.city_name }} {{ address.region_name }}
        {{ address.detail_address }}
      </div>
      <van-icon class="arrow" name="arrow"/>
    </div>
    <div class="good">
      <div class="good-item" v-for="(item, index) in cartList" :key="index">
        <div class="good-img"><img :src="prefix(item.goods_info.img)" alt=""></div>
        <div class="good-desc">
          <div class="good-title">
            <span>{{ item.goods_info.name }}</span>
            <span>x{{ item.buy_num }}</span>
          </div>
          <div class="good-btn">
            <div class="price">¥{{ item.goods_info.price }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="pay-wrap">
      <div class="price">
        <span>商品金额</span>
        <span>¥{{ total }}</span>
      </div>
      <van-button @click="createOrder" class="pay-btn" color="#1baeae" type="primary" block>提交订单</van-button>
    </div>
    <van-popup
      closeable
      :close-on-click-overlay="false"
      v-model="showPay"
      position="bottom"
      :style="{ height: '30%' }"
      @close="close"
    >
      <div :style="{ width: '90%', margin: '0 auto', padding: '50px 0' }">
        <van-button :style="{ marginBottom: '10px' }" color="#1989fa" block @click="payOrder(1)">支付宝支付</van-button>
        <van-button color="#4fc08d" block @click="payOrder(2)">微信支付</van-button>
      </div>
    </van-popup>
  </div>
</template>

<script>
import sHeader from '@/components/SimpleHeader'
import {payOrder} from '../service/order'
import {setLocal} from '@/common/js/utils'
import {Toast} from 'vant'


export default {
  components: {
    sHeader
  },
  created() {
    let id = $cookies.get('id')
    this.$axios.get(this.$settings.BASE_URL + 'address/detail/?user_id=' + id,
      {headers: {'Authorization': 'jwt ' + this.$cookies.get('token')}}
    ).then(res => {
        this.address_list = res.result
      }
    )
  },
  data() {
    return {
      cartList: [],
      address: {},
      showPay: false,
      orderNo: '',
      cartItemIds: [],
      address_list: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      Toast.loading({message: '加载中...', forbidClick: true});
      this.cartList = this.$store.state.params
      this.address = this.$store.state
      const id = this.$cookies.get('id')
      if (!this.$store.state.address_id) {
        this.$axios.get(this.$settings.BASE_URL + 'address/ship/?user_id=' + id,
          {headers: {'Authorization': 'jwt ' + this.$cookies.get('token')}}).then(res => {
          this.$store.state.address_id = res.result.id
          this.$store.state.name = res.result.name
          this.$store.state.tel = res.result.tel
          this.$store.state.province_name = res.result.province_name
          this.$store.state.city_name = res.result.city_name
          this.$store.state.region_name = res.result.region_name
          this.$store.state.detail_address = res.result.detail_address
        })
      }
      Toast.clear()
    },
    goTo() {
      this.$router.push({path: '/shippingaddress'})
    }
    ,
    deleteLocal() {
      setLocal('cartItemIds', '')
    },
    async createOrder() {
      const id = this.$cookies.get('id')
      const name = this.$cookies.get('name')
      let token = this.$cookies.get('token')
      if (token) {
        this.$axios.post(this.$settings.BASE_URL + 'orderform/alipay/' + location.search, {
          subject: this.cartList.map(item => item.goods_info.name)[0],
          user_name: name,
          user: id,
          pay_type: 1,
          buy_num: this.cartList.map(item => item.buy_num)[0],
          commodity_list: this.cartList.map(item => item),
          addr_info: this.address,
          addr_id: this.address.address_id,
          order_total_amount: this.total,
        }, {
          headers: {'Authorization': 'jwt ' + token}
        }).then(res => {
          if (res.code == 101) {
            alert(res.msg)
          }
          if (res) {
            open(res.pay_url, '_self')
          } else {
            Toast.fail(
              res.msg,
            );
          }
        })
      } else {
        Toast.fail('您还没有登入请先登入!')
        this.$router.push({path: '/login'})
      }
    },
    close() {
      this.$router.push({path: 'order'})
    }
    ,
    async payOrder(type) {
      Toast.loading
      await payOrder({orderNo: this.orderNo, payType: type})
      this.$router.push({path: 'order'})
    }
  }
  ,
  computed: {
    total: function () {
      let sum = 0
      this.cartList.forEach(item => {
        sum += item.buy_num * item.goods_info.price
      })
      return sum
    }
  }
}
</script>

<style lang="less" scoped>
@import '../common/style/mixin';

.create-order {
  background: #f9f9f9;

  .address-wrap {
    margin-bottom: 20px;
    background: #fff;
    position: relative;
    margin-top: 44px;
    font-size: 14px;
    padding: 15px;
    color: #222333;

    .name, .address {
      margin: 10px 0;
    }

    .arrow {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 20px;
    }

    &::before {
      position: absolute;
      right: 0;
      bottom: 0;
      left: 0;
      height: 2px;
      background: -webkit-repeating-linear-gradient(135deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
      background: repeating-linear-gradient(-45deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
      background-size: 80px;
      content: '';
    }
  }

  .good {
    margin-bottom: 120px;
  }

  .good-item {
    padding: 10px;
    background: #fff;
    display: flex;

    .good-img {
      img {
        .wh(100px, 100px)
      }
    }

    .good-desc {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;
      padding: 20px;

      .good-title {
        display: flex;
        justify-content: space-between;
      }

      .good-btn {
        display: flex;
        justify-content: space-between;

        .price {
          font-size: 16px;
          color: red;
          line-height: 28px;
        }

        .van-icon-delete {
          font-size: 20px;
          margin-top: 4px;
        }
      }
    }
  }

  .pay-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
    padding: 10px 0;
    padding-bottom: 50px;
    border-top: 1px solid #e9e9e9;

    > div {
      display: flex;
      justify-content: space-between;
      padding: 0 5%;
      margin: 10px 0;
      font-size: 14px;

      span:nth-child(2) {
        color: red;
        font-size: 18px;
      }
    }

    .pay-btn {
      position: fixed;
      bottom: 7px;
      right: 0;
      left: 0;
      width: 90%;
      margin: 0 auto;
    }
  }
}
</style>
